//
//  FavoriteViewController.m
//  笔记1
//
//  Created by  on 2022/11/5.
//

#import "FavoriteViewController.h"
#import "HomeCell.h"
#import "FileViewController.h"
#import "EditViewController.h"

@interface FavoriteViewController ()
@property (strong, nonatomic) IBOutlet UIView *toolbar;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *toolbarBottom;
@property (nonatomic, strong) NSMutableArray <Note *>*selectArray;

@end

@implementation FavoriteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self loaddata];
}
- (void)initUI{
    self.selectArray = [NSMutableArray array];
    self.title = self.isFavorite ? LS(@"收藏夹") : LS(@"废纸篓");
    self.toolbarBottom.constant = -100;
    self.rightBtn.hidden = self.isFavorite;
    [self.rightBtn setTitle:LS(@"管理") forState:UIControlStateNormal];
    [self.rightBtn setTitle:LS(@"完成") forState:UIControlStateSelected];
    [self.rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.collectionView registerNib:[HomeCell nib] forCellWithReuseIdentifier:[HomeCell ID]];
    self.collectionView.c_left.equalTo(self.view);
    self.collectionView.c_right.equalTo(self.view);
    self.collectionView.c_top.equalTo(self.view);
    self.collectionView.c_bottom.equalTo(self.toolbar.c_top);
    [self.view bringSubviewToFront:self.toolbar];
}
- (void)loaddata{
    if (self.isFavorite) {
        [NoteTool findFavorite:^(NSMutableArray<Note *> * _Nonnull result) {
            self.array = result;
            [self.collectionView reloadData];
        }];
    }else{
        [NoteTool findDelete:^(NSMutableArray<Note *> * _Nonnull result) {
            self.array = result;
            [self.collectionView reloadData];
        }];
    }
}
- (void)rightBtnAction{
    self.rightBtn.selected = !self.rightBtn.selected;
    self.toolbarBottom.constant = self.rightBtn.selected ? -20 : -100;
}
- (IBAction)actionForBtn:(UIButton *)sender {
    NSMutableArray *array = [NSMutableArray array];
    for (Note *model in self.array) {
        if (model.select) {
            [array addObject:model];
        }
    }
    if (sender.tag == 201) {
        [CCHud startHud:LS(@"恢复中...")];
        [NoteTool recover:self.selectArray complete:^(BOOL success) {
            [CCHud stopHud:^{}];
        }];
    }else if (sender.tag == 202){
        [CCHud startHud:LS(@"删除中...")];
        [NoteTool delete:self.selectArray complete:^(BOOL success) {
            [CCHud stopHud:^{}];
        }];
    }
}
- (void)addNotify{
    [NotifyTool add:self :@selector(loaddata) :notifyNoteUpdate];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[HomeCell ID] forIndexPath:indexPath];
    Note *model = self.array[indexPath.row];
    cell.model = model;
    cell.favoriteBtn.hidden = YES;
    WS(self)
    cell.block = ^{
        if ([weakself.selectArray containsObject:model]) {
            [weakself.selectArray removeObject:model];
        }else{
            [weakself.selectArray addObject:model];
        }
    };
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    Note *model = self.array[indexPath.row];
    if (model.isFile) {
        FileViewController *vc = [FileViewController new];
        vc.model = model;
        vc.isadd = NO;
        [self push:vc :YES];
        
    }else{
        EditViewController *vc = [EditViewController new];
        vc.model = model;
        vc.isedit = NO;
        [self push:vc :YES];
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat w = (SCREEN_WIDTH - 80) / 3;
    return  CGSizeMake(w, 140.0/92*w+42);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(20, 20, 20, 20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

@end
